﻿using System;
using System.Collections.Generic;
 
using System.Text;
using GGSharp.Structures;

namespace GGSharp.Packets
{
    public class MessagePacket : MessagePacketBase
    {
        protected MessageImageRequest _imageRequest;

        public MessagePacket()
        {
            this.Type = PacketType.GG_SEND_MSG;
            this.Length = 12;
        }

        public MessagePacket(int recipient, string message)
            : this()
        {
            _recipient = recipient;
            _message = message;
        }

        public MessageImageRequest ImageRequest
        {
            get { return _imageRequest; }
            set { _imageRequest = value; }
        }

        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            _recipient = reader.ReadInt32();
            _seq = reader.ReadInt32();
            _class = reader.ReadInt32();

            reader.ReadBytes(this.Length - 12); // - dlugosc poprzednich 3 pol
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            this.Length += _message.Length + 1;
            if (_imageRequest != null)
                this.Length += _imageRequest.Length;

            base.Write(writer);

            writer.Write(_recipient);
            writer.Write(_seq);
            writer.Write(_class);
            writer.Write(Encoding.GetEncoding("windows-1250").GetBytes(_message));
            writer.Write('\0');

            if (_imageRequest != null)
                _imageRequest.Write(writer);
        }
    }
}
